﻿namespace Oddo.Concept.Crm.Client.Core
{
    using System;
    using System.Windows.Input;
    using System.Windows.Media.Imaging;
    using DevExpress.Xpf.Core;
    using Microsoft.Practices.Prism.Commands;
    using Microsoft.Practices.Prism.Modularity;
    using Microsoft.Practices.Prism.Regions;
    using Microsoft.Practices.Prism.UnityExtensions;
    using Microsoft.Practices.Unity;
    using Oddo.Concept.Crm.Client.Core.Resources;
    using Oddo.Concept.Crm.Client.Core.Views;
    using Oddo.Concept.Crm.Common.Attributes;
    using Oddo.Concept.Crm.Common.Modules;
    using Oddo.Concept.Crm.Common.Presentation.Commands;
    using Oddo.Concept.Crm.Common.Presentation.Constants;
    using Oddo.Concept.Crm.Common.Presentation.Infrastructure;
    using Oddo.Concept.Crm.Common.Presentation.Interfaces;
    using Oddo.Concept.Crm.Common.Presentation.Services;
    using Oddo.Concept.Crm.Common.Security;
    
   
    [ModuleAccess]
    [Module(ModuleName = "Core")]
    public class CoreModule : ClientModuleBase
    {
        [Dependency]
        public IShellView ShellView { get; set; }

        public override void RegisterTypes(IUnityContainer container){}

        public override void RegisterRegions(IRegionManager regionManager)
        {
            regionManager.RegisterViewWithRegion(Regions.NAVIGATION_REGION, typeof(NavigationGroupView));
        }

        public override void ExtendUserInterface()
        {
            var auth = Container.Resolve<IAuthenticationService>();
            var menuService = Container.TryResolve<IMenuService>();

            if (auth.IsAuthenticated && auth.CurrentIdentity.IsInRole("Administrator"))
                menuService.AddMainMenuItem(new MenuItem(labels.mnuFile, "_Options", null, null));

            menuService.AddMainMenuItem(
                new MenuItem(labels.mnuFile, labels.mnuExit, new BitmapImage(new Uri("/Oddo.Concept.Crm.Client.Core;component/Images/exit.png", UriKind.Relative)), Commands.ExitCommand, null) { BeginGroup = true });

            var changeThemeCommand = new DelegateCommand<string>(OnChangeThemeCommandHandler);

            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "DXStyle", changeThemeCommand, "DXStyle"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Deep Blue", changeThemeCommand, "DeepBlue"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Light Gray", changeThemeCommand, "LightGray"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Metropolis Dark", changeThemeCommand, "MetropolisDark"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Metropolis Light", changeThemeCommand, "MetropolisLight"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Office2007 Black", changeThemeCommand, "Office2007Black"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Office2007 Blue", changeThemeCommand, "Office2007Blue"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Office2007 Silver", changeThemeCommand, "Office2007Silver"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Office2010 Black", changeThemeCommand, "Office2010Black"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Office2010 Blue", changeThemeCommand, "Office2010Blue"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Office2010 Silver", changeThemeCommand, "Office2010Silver"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Seven", changeThemeCommand, "Seven"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "VS2010", changeThemeCommand, "VS2010"));
            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuThemes, "Office2013", changeThemeCommand, "Office2013"));

            menuService.AddMainMenuItem(CreateMenuItem(labels.mnuHelp, labels.mnuAbout, new BitmapImage(new Uri("/Oddo.Concept.Crm.Client.Core;component/Images/about.png", UriKind.Relative)), null, null));

            var statusbarService = Container.TryResolve<IBarService>();
            statusbarService.AddStatusBarItem(new StatusBarItem() { Content = auth.UserName, ToolTip = labels.label_current_user, Image = new BitmapImage(new Uri("/Oddo.Concept.Crm.Client.Core;component/Images/user.png", UriKind.Relative)) });
            statusbarService.AddStatusBarItem(new StatusBarItem() { Content = auth.UserRoles, ToolTip = labels.label_user_roles });
            statusbarService.AddStatusBarItem(new StatusBarItem() { ToolTip = labels.label_informations });
        }

        private static MenuItem CreateMenuItem(string parent, string title, BitmapImage image, ICommand command, object commandParameter)
        {
            return new MenuItem(parent, title, image, command, commandParameter);
        }

        private static MenuItem CreateMenuItem(string parent, string title, ICommand command, object commandParameter)
        {
            return new MenuItem(parent, title, command, commandParameter);
        }

        private void OnChangeThemeCommandHandler(string themeName)
        {
            if (string.IsNullOrWhiteSpace(themeName))
                themeName = "None";
            ThemeManager.SetThemeName((DXWindow)ShellView, themeName);
        }
        
    }
}